package cn.ibobo.lease.app.controller;

import cn.ibobo.lease.app.service.impl.UserBalanceServiceImpl;
import cn.ibobo.lease.common.enums.ResultEnum;
import cn.ibobo.lease.common.util.ListCopyToVOUtil;
import cn.ibobo.lease.common.util.PaginationVOUtil;
import cn.ibobo.lease.common.vo.ResultVO;
import cn.ibobo.lease.common.util.ResultVOUtil;
import cn.ibobo.lease.common.dto.UserBalanceDTO;
import cn.ibobo.lease.common.vo.UserBalanceVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;

/**
 * @Description 用户点金记录表控制器
 * @Author lililiang
 * @Date 2019-02-16 18:41:19
 */

@RestController
@Api(tags = "充值记录")
@RequestMapping("userBalance")
public class UserBalanceController{

    private static final Log LOGGER = LogFactory.getLog(UserBalanceController.class);

    @Autowired
    private UserBalanceServiceImpl service;
    @Autowired
    private ResultVOUtil<Object> resultObject;

    /**
	 * 用户充值记录表
     * @Author lililiang
     * @Date 2019-02-16 18:41:19
	 * @Param
	 * @Return
	 */
    @ApiOperation(value = "充值记录", response = UserBalanceVO.class)
    @PostMapping("list4Page")
    public ResultVO<Object> list4Page(
            UserBalanceVO entity,
            @ApiParam(value = "当前页", required = true) @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
            @ApiParam(value = "每页显示条数", required = true) @RequestParam(value = "pageSize", defaultValue = "15") Integer pageSize
    ){
        try {
            UserBalanceDTO entityDTO = new UserBalanceDTO();
            BeanUtils.copyProperties(entity, entityDTO);
            List<UserBalanceDTO> list = service.list4Page(entityDTO, pageNum, pageSize);
            return resultObject.success(ResultEnum.SUCCESS.getCode(), ResultEnum.SUCCESS.getMsg(), PaginationVOUtil.getPaginationVO(list, UserBalanceVO.class, pageNum));
        }catch (Exception e) {
            e.printStackTrace();
            return resultObject.error(ResultEnum.EXCEPTION.getCode(), ResultEnum.EXCEPTION.getMsg());
        }
    }

    /**
     * 用户充值记录表（无分页）
     * @Author lililiang
     * @Date 2019-02-16 18:41:19
     * @Param
     * @Return
     */
//    @ApiOperation(value = "用户充值记录表（无分页）", response = UserBalanceVO.class)
//    @PostMapping("listByCondition")
    public ResultVO<Object> listByCondition(UserBalanceVO entity){
        try {
            UserBalanceDTO entityDTO = new UserBalanceDTO();
            BeanUtils.copyProperties(entity, entityDTO);
            List<UserBalanceDTO> list = service.listByCondition(entityDTO);
            return resultObject.success(ResultEnum.SUCCESS.getCode(), ResultEnum.SUCCESS.getMsg(), ListCopyToVOUtil.getListToVO(list, UserBalanceVO.class));
        }catch (Exception e) {
            e.printStackTrace();
            return resultObject.error(ResultEnum.EXCEPTION.getCode(), ResultEnum.EXCEPTION.getMsg());
        }
    }

    /**
     * 用户点金记录表详细信息
     * @Author lililiang
     * @Date 2019-02-16 18:41:19
     * @Param
     * @Return
     */
//    @ApiOperation(value = "用户点金记录表详细信息", response = UserBalanceVO.class)
//    @PostMapping("queryOneByPk")
    public ResultVO<Object> queryOneByPk(@ApiParam(value = "id",required = true) @RequestParam(value = "id") String id){
        try {
            UserBalanceDTO entityDTO = service.queryOneByPk(id);
            UserBalanceVO entity = new UserBalanceVO();
            BeanUtils.copyProperties(entityDTO, entity);
            return resultObject.success(ResultEnum.SUCCESS.getCode(), ResultEnum.SUCCESS.getMsg(), entity);
        }catch (Exception e) {
            e.printStackTrace();
            return resultObject.error(ResultEnum.EXCEPTION.getCode(), ResultEnum.EXCEPTION.getMsg());
        }
    }

    /**
     * 保存用户点金记录表信息
     * @Author lililiang
     * @Date 2019-02-16 18:41:19
     * @Param
     * @Return
     */
//    @ApiOperation(value = "保存用户点金记录表信息", response = ResultVO.class)
//    @PostMapping("save")
    public ResultVO<Object> save(UserBalanceVO entity) {
        try {
            UserBalanceDTO entityDTO = new UserBalanceDTO();
            BeanUtils.copyProperties(entity, entityDTO);
            if(service.save(entityDTO) > 0) {
                return resultObject.success(ResultEnum.SUCCESS.getCode(), ResultEnum.SUCCESS.getMsg());
            }else {
                return resultObject.error(ResultEnum.FAIL.getCode(), ResultEnum.FAIL.getMsg());
            }
        }catch (Exception e) {
            e.printStackTrace();
            return resultObject.error(ResultEnum.EXCEPTION.getCode(), ResultEnum.EXCEPTION.getMsg());
        }
    }

    /**
     * 删除用户点金记录表信息
     * @Author lililiang
     * @Date 2019-02-16 18:41:19
     * @Param
     * @Return
     */
//    @ApiOperation(value = "删除用户点金记录表信息", response = ResultVO.class)
//    @PostMapping("delete")
    public ResultVO<Object> delete(@ApiParam(value = "id",required = true) @RequestParam(value = "id") String id){
        try {
            if(service.delete(id) > 0) {
                return resultObject.success(ResultEnum.SUCCESS.getCode(), ResultEnum.SUCCESS.getMsg());
            }else {
                return resultObject.success(ResultEnum.FAIL.getCode(), ResultEnum.FAIL.getMsg());
            }
        }catch (Exception e) {
            e.printStackTrace();
            return resultObject.error(ResultEnum.EXCEPTION.getCode(), ResultEnum.EXCEPTION.getMsg());
        }
    }

}